package com.spark.usermusicService.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spark.usermusicService.entity.User;
import com.spark.usermusicService.mapper.UserMapper;
import com.spark.usermusicService.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 用户信息 服务实现类
 * </p>
 *
 * @author wzk
 * @since 2021-07-01
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Override
    public User getUserById(String id) {
        return baseMapper.selectById(id);
    }

    @Override
    public User getUserByNickName(String nickName) {
        return baseMapper.selectOne(new QueryWrapper<User>().eq("nickname", nickName));
    }

    @Override
    public void insertUser(User user){
        baseMapper.insert(user);
    }

    @Autowired
    private LoveMusicServiceImpl loveMusicService;

    @Autowired
    private PlaylistServiceImpl playlistService;

    @Override
    public User getInfoById(String id){
//        User user = userMapper.selectById(id);
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.select(User.class, info -> !info.getColumn().equals("password")).eq("id",id);
        User user = getOne(queryWrapper);
        return user;
    }

    @Transactional
    @Override
    public boolean deleteById(String id) {
        boolean b = loveMusicService.deleteLoveMusicByUserId(id);
        boolean b1 = playlistService.deletePlaylistByUserId(id);
        boolean b2 = removeById(id);
        return b&&b1&&b2;
    }
}
